package com.zmn.plat.admin.controller.netcall;

import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.netcall.UserImAccountBService;
import com.zmn.plat.model.entity.netcall.UserImAccount;
import com.zmn.plat.model.vo.netcall.ImAccountVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 网易云信接入
 * 
 * @author sunlife
 * @since 2019/7/12 16:57
 */
@Controller
@RequestMapping("/netcall/im")
public class ImController extends PlatBaseController {
    private Logger logger = LoggerFactory.getLogger(ImController.class);
    @Resource
    private UserImAccountBService userImAccountBService;

    // @RequiresPermissions("/netcall/im/connect.action")
    @GetMapping(value = "/info")
    @ResponseBody
    public ResultDTO imInfo(HttpServletRequest request, @RequestParam("mastId") Integer mastId) {
        try {
            Integer staffId = getMcStaffId();
            UserImAccount staffImAccount = userImAccountBService.getAccountByStaffId(staffId);
            UserImAccount mastImAccount = userImAccountBService.getAccountByStaffId(mastId);

            if (Objects.isNull(staffImAccount) || Objects.isNull(mastImAccount)) {
                return ResultDTO.fail("获取通讯信息失败");
            }

            ImAccountVO accountVO = new ImAccountVO();
            accountVO.setStaffImId(staffImAccount.getImAcctId());
            accountVO.setStaffImToken(staffImAccount.getImToken());
            accountVO.setMastImId(mastImAccount.getImAcctId());

            return ResultDTO.success(accountVO);
        } catch (Exception e) {
            logger.info("获取通信信息失败" + e.getMessage());
            return ResultDTO.fail(e.getMessage());
        }
    }

    // @RequiresPermissions("/netcall/im/connect.action")
    @GetMapping(value = "/connect")
    public String connect() {
        return "netcall/netcall/dist/index";
    }
}
